package com.example.aslongastoday.adapter;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.provider.CalendarContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.RecyclerView;

import com.example.aslongastoday.R;
import com.example.aslongastoday.bean.BeanFactory;
import com.example.aslongastoday.databinding.ViewBacklogBinding;
import com.example.aslongastoday.db.dao.BackLogDao;
import com.example.aslongastoday.db.dao.BackLogGroupDao;
import com.example.aslongastoday.domain.BackLog;
import com.example.aslongastoday.domain.BackLogGroup;
import com.example.aslongastoday.ui.backlog.EditBacklogFragment;
import com.example.aslongastoday.ui.backlog.SetTimeFragment;

import java.util.GregorianCalendar;
import java.util.List;

public class ViewBackLogAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Integer backLogGroupId;
    private List<BackLog> backLogList;
    private final Context context;
    private final BackLogDao dao;
    private final Fragment fragment; //这里的主fragment是主待办模块的那个fragment

    public ViewBackLogAdapter(List<BackLog> item, Context context, Integer backLogGroupId, Fragment fragment) {
        this.backLogList = item;
        this.context = context;
        this.backLogGroupId = backLogGroupId;
        this.fragment=fragment;
        dao = BackLogDao.getInstance(context);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(ViewBacklogBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder1, int position) {
        ViewHolder holder = (ViewHolder) holder1;
        holder.context.setText(backLogList.get(position).getContent());

        /**
         * 以下的注释为演示backLog修改的样例 ， 其他样例在单元测试里有
         */


        int i = position;

        //完成按钮事件监听
        holder.iv_finished.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //在这里实现待办的完成
                BackLogDao backLogDao = BackLogDao.getInstance(context);
                backLogDao.recycle(backLogGroupId,backLogList.get(i).getPosition());
                setBacklogGroupDataChange();
                Toast.makeText(context, "删除完成", Toast.LENGTH_SHORT).show();
            }
        });

        //定时提醒事件监听
        holder.iv_clock.setOnClickListener(new View.OnClickListener() {

            private SetTimeFragment setTimeFragment;

            @Override
            public void onClick(View v) {
                Intent calIntent = new Intent(Intent.ACTION_INSERT);
                calIntent.setType("vnd.android.cursor.item/event");
                calIntent.putExtra(CalendarContract.Events.TITLE, backLogList.get(i).getContent());
                calIntent.putExtra(CalendarContract.Events.EVENT_LOCATION, "Location here");
                calIntent.putExtra(CalendarContract.Events.DESCRIPTION, "Description here");
                GregorianCalendar calDate = new GregorianCalendar(2023, 5, 1);
                calIntent.putExtra(CalendarContract.EXTRA_EVENT_ALL_DAY, true);
                calIntent.putExtra(CalendarContract.EXTRA_EVENT_BEGIN_TIME,
                        calDate.getTimeInMillis());
                calIntent.putExtra(CalendarContract.EXTRA_EVENT_END_TIME,
                        calDate.getTimeInMillis());
                try{
                    context.startActivity(calIntent);
                }catch (Exception e){
                    Toast.makeText(context,"抱歉，您的手机里未安装日程表",Toast.LENGTH_SHORT).show();
                }
            }
        });

        //长按编辑待办项
        holder.backlogItemContext.setOnLongClickListener(new View.OnLongClickListener() {

            private FragmentTransaction fragmentTransaction;
            private FragmentManager fragmentManager;
            private String type;
            private Bundle bundle;
            private EditBacklogFragment editBacklogFragment;

            @Override
            public boolean onLongClick(View v) {
                //跳转到编辑页面
                //新建一个实例化对象
                editBacklogFragment = new EditBacklogFragment();
                //数据打包
                bundle = new Bundle();
                type = "edit_backlog";
                bundle.putString("type", type);
                bundle.putString("groupId", String.valueOf(backLogList.get(i).getGroupId()));
                bundle.putString("id", String.valueOf(backLogList.get(i).getPosition()));
                bundle.putString("text", backLogList.get(i).getContent());
                editBacklogFragment.setArguments(bundle);
                // 跳转到EditBacklogFragment
                fragmentManager = fragment.getActivity().getSupportFragmentManager();
                fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.container, editBacklogFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.commit();
                return true;
            }
        });

    }

    /**
     * 更新数据视图
     */
    private void setBacklogGroupDataChange(){
        ViewBackLogGroupAdapter viewBackLogGroupAdapter = (ViewBackLogGroupAdapter) BeanFactory.getBean("viewBackLogGroupAdapter");
        BackLogGroupDao backLogGroupDao = BackLogGroupDao.getInstance(context);
        List<BackLogGroup> backLogGroupList = backLogGroupDao.queryUnfinishedAll();
        viewBackLogGroupAdapter.setBackLogGroupList(backLogGroupList);
        viewBackLogGroupAdapter.notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        //暂时补充的空值的处理情况(返回长度为0)
        return (backLogList == null) ? 0 : backLogList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        public final TextView context;
//        public final TextView mContext;
//        public final TextView mDDL;
//        public BackLog mItem;
//        public final RecyclerView backlogItemList;

        //控件绑定
        public final ImageView iv_finished;
        private final ImageView iv_clock;
        private final TextView backlogItemContext;

        public ViewHolder(ViewBacklogBinding binding) {
            super(binding.getRoot());
            context = binding.backlogItemContext;
//            backlogItemList = binding.backlogItemContext;
//            mContext = binding.content;
//            mDDL = binding.ddl;

            //待办删除和定时提醒按钮绑定
            iv_finished = binding.ivFinished;
            iv_clock = binding.ivClock;
            backlogItemContext = binding.backlogItemContext;

        }

        @Override
        public String toString() {
            return "";
        }
    }


}
